package tt.lab.pmbok.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import tt.lab.pmbok.dto.ArtifactAttributeDTO;
import tt.lab.pmbok.dto.ArtifactTemplateDTO;
import tt.lab.pmbok.model.ArtifactAttribute;
import tt.lab.pmbok.model.ArtifactTemplate;
import tt.lab.pmbok.service.ArtifactService;

@Controller
// 类似Struts的Action
public class ArtifactController {
	/**/
	@Resource(name = "artifactService")
	// 获取applicationContext.xml中bean的id为loginService的，并注入
	private ArtifactService artifactService; // 等价于spring传统注入方式写get和set方法，这样的好处是简洁工整，省去了不必要得代码

	@RequestMapping("/artifacts")
	public void retrieveAll(Model model) {
		
	}
	@RequestMapping("/artifact/{id}")
	public void retrieve(@PathVariable int id, Model model) {
		
	}
	@RequestMapping("/artifact/template/{id}")
	public void retrieveTemplate(@PathVariable int id, Model model) {
		ArtifactTemplate template = artifactService.loadTemplate(id);
		ArtifactTemplateDTO artifactTemplateDTO = new ArtifactTemplateDTO(template.getId(),template.getName());
		
		for(ArtifactAttribute attribute: template.getAttributes()){
			ArtifactAttributeDTO artifactAttributeDTO = new ArtifactAttributeDTO(attribute.getId(),attribute.getAttributeId(),attribute.getAttributeName(),attribute.getAttributeType(),attribute.getAttributeValues());
			artifactTemplateDTO.addAttribute(artifactAttributeDTO);
		}
		
		model.addAttribute("template", artifactTemplateDTO);
	}
}